package com.cykj.eurekaclient.service.impl;

import com.cykj.eurekaclient.bean.FrontUser;
import com.cykj.eurekaclient.mapping.FrontUserMapper;
import com.cykj.eurekaclient.service.FrontUserService;
import com.cykj.eurekaclient.utils.FtpTool;
import com.cykj.eurekaclient.utils.wx_code.WXTool;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import sun.misc.BASE64Decoder;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.util.Map;

@Service
public class FrontUserServiceImpl implements FrontUserService {

    @Autowired
    FrontUserMapper frontUserMapper;

    /**
     * @version 1.0
     * @author: zhiMing
     * @date: 2021/9/24 17:00
     * @desc: 处理有关前端用户登录请求,利用工具类进行获取微信数据：openId等
     */
    @Override
    public Map<String,Object> frontUserLogin(FrontUser frontUser,String code) {
        System.out.println(code);
        Map<String,Object> map = WXTool.getWXInfoByCode(code);
        String unionId = (String) map.get("unionId");
        String sessionKey = (String) map.get("sessionKey");
        String openId = (String) map.get("openId");
        if (unionId!=null || openId!=null){
            /*1，判断用户的微信信息是否已经存在*/
            FrontUser codeUser = frontUserMapper.getFrontUserByWXInfo(new FrontUser(unionId,openId));
            if (codeUser == null){/* null即第一次登录 */
                try {
                    frontUser.setWxUnionId(unionId);
                    frontUser.setWxOpenId(openId);
                    frontUser.setWxSessionKey(sessionKey);
                    frontUserMapper.insertByUserInfo(frontUser);
                    /* 清空map内的信息，将用户信息传回前端 */
                    map.clear();
                    map.put("frontUserInfo", frontUser);
                }catch (Exception e){
                    e.printStackTrace();
                    /* 信息注入数据库失败 */
                    map.clear();
                    map.put("err","-100");
                }
            }else {
                /* 清空map内的信息，将用户信息传回前端 */
                map.clear();
                map.put("frontUserInfo", codeUser);
                System.out.println("不是第一次登录");
            }
        }else {
            /* 前端传递的code有误 */
            map.clear();
            map.put("err","-101");
        }
        return map;
    }

    @Override
    public FrontUser updateFrontUserAvatar(FrontUser frontUser, String fileContent, String fileSuffix) {
        String base64Str = "";
        String ftpMsg = "";
        if (fileContent!=null){
            base64Str = fileContent.replaceFirst("data:.*;base64,","");
        }
        BASE64Decoder decoder = new BASE64Decoder();
        try {
            byte[] bytes = decoder.decodeBuffer(base64Str);
            for (int i=0;i<bytes.length;i++){
                if (bytes[i] < 0){
                    bytes[i] += 256;
                }
            }
            /* 利用用户的id拼接出用户对象的文件名 */
            String imageName = "frontUser-avatar"+frontUser.getId();
            ftpMsg = FtpTool.updateImage(imageName,fileSuffix,new ByteArrayInputStream(bytes));
        } catch (IOException e) {
            e.printStackTrace();
        }
        if (!ftpMsg.equals("-1") && !ftpMsg.equals("-2") && !ftpMsg.equals("-3")){
            try {
                frontUserMapper.updateFrontUserAvatar(frontUser,"http"+"://120.77.43.188/"+ftpMsg);
            }catch (Exception e){
                e.printStackTrace();
                return null;
            }
        }
        return frontUserMapper.getFrontUserByWXInfo(frontUser);
    }

    /**
     * @version 1.0
     * @author: zhiMing
     * @date: 2021/9/24 17:00
     * @desc: 处理有关乘客更换头像
     */
    @Override
    public FrontUser updateFrontUserAvatarUrl(FrontUser frontUser, String fileUrl, String fileSuffix) {
        try {
            frontUserMapper.updateFrontUserAvatar(frontUser,fileUrl);
        }catch (Exception e){
            e.printStackTrace();
            return null;
        }
        return frontUserMapper.getFrontUserByWXInfo(frontUser);
    }
}
